package com.cdhkppm.controller;


import com.alibaba.fastjson.JSON;
import com.cdhkppm.dto.CustomPlusTaskSaveDTO;
import com.cdhkppm.service.ExportTaskToExcelService;
import com.cdhkppm.service.impl.CustomTyppmImportDataServiceImpl;
import com.hustcad.plm.pdm.core.constant.ApiPathConstant;
import com.hustcad.plm.rpm.model.dto.active.PlusTaskSaveDTO;
import com.ty.basic.response.ResponseResult;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author yfq
 * @apiNote
 */
@RestController
@Api(tags = "根据用户导入导出Export")
@RequestMapping(ApiPathConstant.PARENT_VERSION_ONE_PATH + "exportTask")
public class exportTaskToExcelController {

    @Resource
    private ExportTaskToExcelService exportPlanActiveToExcel;


    @PostMapping("/exportTaskToExcel")
    public void exportPlanActiveToExcel(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestParam("tasksJson") String tasksJson,
            @RequestParam("pjoName") String pjoName
    ) throws IOException {
        List<CustomPlusTaskSaveDTO> exportUserTaskDTOList =
                JSON.parseArray(tasksJson, CustomPlusTaskSaveDTO.class);
        exportPlanActiveToExcel.exportTaskToExcel(request, response, exportUserTaskDTOList,pjoName);
    }


    @PostMapping("/importSubTaskFromExcel")
    public ResponseResult importSubTaskFromExcel(@RequestParam("file") MultipartFile file,
                                                 @RequestParam("tasksJson") String tasksJson,
                                                 @RequestParam("importType") String importType){

        List<CustomPlusTaskSaveDTO> exportUserTaskDTOList =
                JSON.parseArray(tasksJson, CustomPlusTaskSaveDTO.class);
        if ("normal".equalsIgnoreCase(importType)){
            exportPlanActiveToExcel.importSubTaskFromExcel(file, exportUserTaskDTOList);
        }
        if ("devSub".equalsIgnoreCase(importType)){
            exportPlanActiveToExcel.importDevSubSubTaskFromExcel(file, exportUserTaskDTOList);
        }
        return ResponseResult.success();
    }
}
